home *** CD-ROM | disk | FTP | other *** search
/ User's Choice Windows CD / User's Choice Windows CD (CMS Software)(1993).iso / utility1 / gs261src.zip / SFILTER2.C < prev    next >
C/C++ Source or Header  |  1993-05-13  |  8KB  |  292 lines

  1. /* Copyright (C) 1991, 1992, 1993 Aladdin Enterprises.  All rights reserved.
  2.  
  3. This file is part of Ghostscript.
  4.  
  5. Ghostscript is distributed in the hope that it will be useful, but
  6. WITHOUT ANY WARRANTY.  No author or distributor accepts responsibility
  7. to anyone for the consequences of using it or for whether it serves any
  8. particular purpose or works at all, unless he says so in writing.  Refer
  9. to the Ghostscript General Public License for full details.
  10.  
  11. Everyone is granted permission to copy, modify and redistribute
  12. Ghostscript, but only under the conditions described in the Ghostscript
  13. General Public License.  A copy of this license is supposed to have been
  14. given to you along with Ghostscript so you can know your rights and
  15. responsibilities.  It should be in a file named COPYING.  Among other
  16. things, the copyright notice and this notice must be preserved on all
  17. copies.  */
  18.  
  19. /* sfilter2.c */
  20. /* Additional stream functions for filters */
  21. #include "stdio_.h"    /* includes std.h */
  22. #include "memory_.h"
  23. #include "scanchar.h"
  24. #include "stream.h"
  25.  
  26. /* Generic functions in sfilter.c */
  27. extern int s_filter_write_flush(P1(stream *));
  28. extern int s_filter_close(P1(stream *));
  29.  
  30. /* ------ ASCII85Encode ------ */
  31.  
  32. /* Flush the buffer */
  33. private int
  34. s_A85E_write_buf(register stream *s)
  35. {    register stream *strm = s->strm;
  36.     register byte *p = s->cbuf;
  37.     register int count = s->cptr + 1 - p;
  38.     while ( count >= 4 )
  39.       { ulong word =
  40.           ((ulong)(((uint)p[0] << 8) + p[1]) << 16) +
  41.           (((uint)p[2] << 8) + p[3]);
  42.         if ( word == 0 )
  43.           sputc(strm, 'z');
  44.         else
  45.           { ulong q = word / (85L*85*85*85);  /* actually only a byte */
  46.         ushort w1;
  47.         sputc(strm, (byte)q + '!');
  48.         word -= q * (85L*85*85*85);
  49.         q = word / (85L*85*85);
  50.         sputc(strm, (byte)q + '!');
  51.         word -= q * (85L*85*85);
  52.         q = word / (85*85);
  53.         sputc(strm, (byte)q + '!');
  54.         w1 = (ushort)(word - q * (85L*85));
  55.         sputc(strm, (byte)(w1 / 85) + '!');
  56.         sputc(strm, (byte)(w1 % 85) + '!');
  57.         if ( !(count & 60) )
  58.           sputc(strm, '\n');
  59.           }
  60.         count -= 4, p += 4;
  61.       }
  62.     memcpy(s->cbuf, p, count);
  63.     s->cptr = s->cbuf + count - 1;
  64.     return 0;
  65. }
  66.  
  67. /* Close the stream, flushing a partial word. */
  68. private int
  69. s_A85E_close(register stream *s)
  70. {    stream *strm = s->strm;
  71.     int count;
  72.     (*s->procs.write_buf)(s);
  73.     count = s->cptr - s->cbuf + 1;
  74.     if ( count > 0 )
  75.        {    /* Handle leftover bytes.  1 <= count <= 3. */
  76.         /* All the bytes are at the beginning of the buffer. */
  77.         byte ebuf[5];
  78.         stream sst;
  79.         s->cptr[1] = s->cptr[2] = s->cptr[3] = 0;
  80.         s->cptr = s->cbuf + 3;
  81.         swrite_string(&sst, ebuf, 5);
  82.         s->strm = &sst;
  83.         (*s->procs.write_buf)(s);    /* force out final codes */
  84.         if ( ebuf[0] == 'z' )        /* don't use as last code */
  85.             memcpy(ebuf, "!!!!", 4);
  86.         sputs(strm, ebuf, count + 1);
  87.        }
  88.     sputs(strm, (byte *)"~>", 2);
  89.     return s_std_close(s);
  90. }
  91.  
  92. /* Stream procedures */
  93. const stream_procs s_A85E_procs =
  94.    {    s_std_noavailable, s_std_noseek, s_filter_write_flush, s_A85E_close,
  95.     NULL, s_A85E_write_buf
  96.    };
  97.  
  98. /* ------ ASCII85Decode ------ */
  99.  
  100. /* Refill the buffer */
  101. private int
  102. s_A85D_read_buf(register stream *s)
  103. {    register stream *strm = s->strm;
  104.     register byte *p = s->cbuf;
  105.     byte *limit = p + s->bsize - 4;
  106.     int ccount = 0;
  107.     ulong word = 0;
  108.     for ( ; ; )
  109.       { int ch = sgetc(strm);
  110.         uint ccode = ch - '!';
  111.         if ( ccode < 85 )        /* catches ch < '!' as well */
  112.           { if ( p >= limit )
  113.          { sputback(strm);
  114.            break;
  115.          }
  116.         word = word * 85 + ccode;
  117.         if ( ++ccount == 5 )
  118.          { p[0] = word >> 24;
  119.            p[1] = (byte)(word >> 16);
  120.            p[2] = (byte)((uint)word >> 8);
  121.            p[3] = (byte)word;
  122.            p += 4;
  123.            word = 0;
  124.            ccount = 0;
  125.          }
  126.           }
  127.         else if ( ch == 'z' && ccount == 0 )
  128.          { if ( p >= limit )
  129.         { sputback(strm);
  130.           break;
  131.         }
  132.            p[0] = p[1] = p[2] = p[3] = 0,
  133.            p += 4;
  134.          }
  135.         else if ( scan_char_decoder[ch] == ctype_space )
  136.           ;
  137.         else if ( ch == '~' && sgetc(strm) == '>' )
  138.          { /* Handle odd bytes.  We always have room for them, */
  139.            /* because limit is 4 bytes before the end of the buffer. */
  140.            s->end_status = EOFC;
  141.            switch ( ccount )
  142.            {
  143.         case 0:
  144.             break;
  145.         case 1:            /* syntax error */
  146.             s->end_status = ERRC;
  147.             break;
  148.         case 2:            /* 1 odd byte */
  149.             word = word * (85L*85*85) + 0xffffffL;
  150.             goto o1;
  151.         case 3:            /* 2 odd bytes */
  152.             word = word * (85L*85) + 0xffffL;
  153.             goto o2;
  154.         case 4:            /* 3 odd bytes */
  155.             word = word * 85 + 0xffL;
  156.             p[2] = (byte)(word >> 8);
  157. o2:            p[1] = (byte)(word >> 16);
  158. o1:            p[0] = (byte)(word >> 24);
  159.             p += ccount - 1;
  160.            }
  161.            break;
  162.          }
  163.         else            /* syntax error or exception */
  164.          { s->end_status = (ch < 0 ? ch : ERRC);
  165.            break;
  166.          }
  167.       }
  168.     s->cptr = s->cbuf - 1;
  169.     s->endptr = p - 1;
  170.     return 0;
  171. }
  172.  
  173. /* Stream procedures */
  174. const stream_procs s_A85D_procs =
  175.    {    s_std_noavailable, s_std_noseek, s_std_read_flush, s_filter_close,
  176.     s_A85D_read_buf, NULL
  177.    };
  178.  
  179. /* ------ RunLengthEncode ------ */
  180.  
  181. /* Initialize */
  182. void
  183. s_RLE_init(register stream *s, uint rec_size)
  184. {    s->record_size = (rec_size == 0 ? max_uint : rec_size);
  185.     s->record_left = s->record_size;
  186. }
  187.  
  188. /* Empty the buffer */
  189. private int
  190. s_RLE_write_buf(register stream *s)
  191. {    register stream *strm = s->strm;
  192.     register byte *p = s->cbuf;
  193.     while ( p <= s->cptr )
  194.        {    byte *beg = p, *q;
  195.         uint count = s->cptr - p + 1;
  196.         if ( count > s->record_left)
  197.             count = s->record_left;
  198.         if ( count > 127 )
  199.             count = 127;
  200.         q = p + count;
  201.         if ( count > 2 && p[1] == p[0] )
  202.            {    /* Recognize leading repeated byte */
  203.             do { p++; }
  204.             while ( p + 1 < q && p[1] == p[0] );
  205.             p++;
  206.             sputc(strm, (byte)(257 - (p - beg)));
  207.             sputc(strm, *beg);
  208.            }
  209.         else
  210.            {    while ( p + 2 < q && (p[1] != p[0] || p[2] != p[0]) )
  211.                 p++;
  212.             if ( p + 2 >= q ) p = q;
  213.             sputc(strm, (byte)(p - beg - 1));
  214.             sputs(strm, beg, p - beg);
  215.            }
  216.         s->record_left -= p - beg;
  217.         if ( s->record_left == 0 )
  218.             s->record_left = s->record_size;
  219.        }
  220.     s->cptr = s->cbuf - 1;
  221.     return 0;
  222. }
  223.  
  224. /* Close */
  225. private int
  226. s_RLE_close(register stream *s)
  227. {    (*s->procs.write_buf)(s);
  228.     sputc(s->strm, 128);
  229.     return s_std_close(s);
  230. }
  231.  
  232. /* Stream procedures */
  233. const stream_procs s_RLE_procs =
  234.    {    s_std_noavailable, s_std_noseek, s_filter_write_flush, s_RLE_close,
  235.     NULL, s_RLE_write_buf
  236.    };
  237.  
  238. /* ------ RunLengthDecode ------ */
  239.  
  240. /* Initialize */
  241. void
  242. s_RLD_init(register stream *s)
  243. {    s->odd = -1;
  244. }
  245.  
  246. /* Refill the buffer */
  247. private int
  248. s_RLD_read_buf(register stream *s)
  249. {    register stream *strm = s->strm;
  250.     register byte *p = s->cbuf;
  251.     byte *limit = p + s->bsize;
  252.     int b = s->odd;
  253.     if ( b < 0 ) b = sgetc(strm);
  254.     for ( ; ; )
  255.        {    uint count;
  256.         if ( b < 0 ) break;    /* EOF/ERR */
  257.         if ( b < 128 )
  258.            {    if ( b >= limit - p )    /* data won't fit */
  259.                 break;
  260.             count = sgets(strm, p, b + 1);
  261.             p += count;
  262.             b = -1;
  263.             if ( count == 0 && strm->end_status )    /* EOF/ERR */
  264.                 break;
  265.            }
  266.         else if ( b == 128 )    /* end of data */
  267.            {    s->end_status = EOFC;
  268.             b = -1;
  269.             break;
  270.            }
  271.         else if ( (count = 257 - b) > limit - p )
  272.             break;        /* won't fit */
  273.         else
  274.            {    b = sgetc(strm);
  275.             if ( b < 0 ) break;    /* EOF/ERR */
  276.             memset(p, b, count);
  277.             p += count;
  278.            }
  279.         b = sgetc(strm);
  280.        }
  281.     s->cptr = s->cbuf - 1;
  282.     s->endptr = p - 1;
  283.     s->odd = b;
  284.     return 0;
  285. }
  286.  
  287. /* Stream procedures */
  288. const stream_procs s_RLD_procs =
  289.    {    s_std_noavailable, s_std_noseek, s_std_read_flush, s_std_close,
  290.     s_RLD_read_buf, NULL
  291.    };
  292.